<?php
class DBConn{
	private static $db_host="localhost";
	private static $db_user="double";
	private static $db_psw="123456";
	private static $db_name="myblog";
	private static $connection;
	static function getConn(){
		$connection=new mysqli(self::$db_host,self::$db_user,self::$db_psw,self::$db_name);
		return $connection;
	}
}

	class UserModel{
		//查询注册用户名是否存在
		function checkRegister ($username){
			$mysqli = DBConn::getConn();
			$sql="select user_name from user where user_name=?";
			$result=$mysqli->prepare($sql);
			$result->bind_param("s",$user_name);
			$user_name=$username;
			$result->execute();.
			$result->bind_result($user_name);
			if($result->fetch()){
				$result->close();
				$mysqli->close();
				return FALSE;
			}else{
				$result->close();
				$mysqli->close();
				return TRUE;
			}
		}
		//储存用户注册信息
		function register($registerArray){
			$mysqli=DBConn::getConn();
			$mysqli->query("set names gb2312");
			$query="insert into user (user_name,user_pw) values(?,?)";
			$result=$mysqli->prepare($query);
			$result->bind_param("ss",$user_name,$user_pw);
			$user_name = $registerArray['user_name'];
			$user_pw = $registerArray['user_pw'];
			$result->execute();
			$mysqli->close();
		}
		//验证用户名密码
		function checkLogin($username,$userpw){
			$mysqli=DBConn::getConn();
			$mysqli->query("set names gb2312");
			$sql="select * from user where user_name=? and user_pw=?";
			$result=$mysqli->prepare($sql);
			$result->bind_param("ss",$user_name,$user_pw);
			$user_pw=$userpw;
			$user_name=$username;
			$result->execute();
			$result->bind_result($user_id,$user_name,$nickname,$user_pw);
			while($result->fetch()){
				$userList=array(
					'user_id'=>$user_id,
					'user_name'=>$user_name,
					'user_pw'=>$user_pw
				);
			}
			$result->close();
			$mysqli->close();
			return $userList;//返回一个数组
		}
		//查询所有用户
		function selectAllUser(){
			$mysqli = DBconn::getConn();
			$mysqli ->query("set names utf-8");
			$sql="select * from user";
			$result = $mysqli->prepare($sql);
			$result->execute();
			$result -> bind_result($user_id,$user_name,$nickname,$user_pw);
			$userArray=array();
			while($result->fetch()){
				$userList=array(
					'nickname'=>$nickname,
					'user_id'=>$user_id,
					'user_name'=>$user_name,
					'user_pw'=>$user_pw
				);
				$userArray[]=$userList;
			}
			$result->close();
			$mysqli->close();
			return $userArray;
		}
	}


?>